/**
 *    Copyright 2009 Thomas Johnson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weareriot.hitprox;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import static com.weareriot.hitprox.ColumnParamMapping.*;


public class HitProxUI extends JPanel {

	private static final Vector<String> columnNames = new Vector<String>();
	static {
		columnNames.add("#");
		// columnNames.add("Name");
		columnNames.add(VERB.getColumnName());
		columnNames.add(NOUN.getColumnName());
	}
	
//	// Uncaptured Events that are expected during this session
//	private Vector<HitboxEvent> pendingEvents = new Vector<HitboxEvent>();
//	private JTable pendingTable = new JTable(placeholderData, placeholderNames);
//	
//	// Captured Events that match the ones expected in the run
//	private Vector<HitboxEvent> verified = new Vector<HitboxEvent>();
//	private JTable verifiedTable = new JTable();
	
	// Captured events that do not match any expected values
	private Vector<Vector<String>> unexpectedEvents = new Vector<Vector<String>>();
	private DefaultTableModel unexpectedModel = new DefaultTableModel(unexpectedEvents, columnNames)  {
		@Override
		public boolean isCellEditable(int row, int column) {
			return false;
		}
	};
	
	private JTable unexpectedTable = new JTable(unexpectedModel);
	private JScrollPane unexpectedScroller = new JScrollPane(unexpectedTable,
			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	
	private static final String EVENT_COUNT_FORMAT = "Events Seen: %d";
	private JLabel eventCountDisplay = new JLabel(String.format(EVENT_COUNT_FORMAT, 0));
	
	public HitProxUI() {
		setLayout(new BorderLayout());
		
		add(new JLabel("Hitbox Events"), BorderLayout.PAGE_START);
		
		unexpectedScroller.setColumnHeaderView(unexpectedTable.getTableHeader());
		
		add(unexpectedScroller, BorderLayout.CENTER);
		
		unexpectedTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		
		TableColumnModel tcm = unexpectedTable.getColumnModel();
		
		for(int i = 0; i < tcm.getColumnCount(); i++) {
			TableColumn col = unexpectedTable.getColumnModel().getColumn(i);
			int width = 240;
			
			switch(i) {
				case 0: width = 20; break; // ID column
				case 1: width = 280; break;
				case 2: width = 200; break;
				default: width = 100; break;
			}
			
			col.setPreferredWidth(width);
		}
		
		updateEventCount();
		add(eventCountDisplay, BorderLayout.PAGE_END);
	}
	
	private void updateEventCount() {
		int total = unexpectedEvents.size(); // + verifiedEvents.size();
		eventCountDisplay.setText(String.format(EVENT_COUNT_FORMAT, total));
	}
	
	public void addEvent(HashMap<String,String> params) {
		DefaultTableModel target = unexpectedModel;
		target.addRow(new Object[] {
			new Integer(target.getRowCount() + 1),
			// params.get("name"),
			params.get(VERB.param()),
			params.get(NOUN.param())
		});
		updateEventCount();
		
		new Thread() {

			@Override
			public void run() {
				super.run();
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) { }
				showLastRow();
			}
		}.start();
	}
	
	private void showLastRow() {
		JTable table = unexpectedTable;
		
		if(!unexpectedEvents.isEmpty()) {
			Rectangle view = table.getCellRect(unexpectedModel.getRowCount() - 1, 0, true);
			
			table.scrollRectToVisible(view);
		}
	}
}
